import Koa from 'koa';
import Router from 'koa-router';
import send from 'koa-send';
import path from 'path';
import errorHandler from './error-handler';
import proxy from './proxy';

const app = new Koa();
const router = new Router();

// error handler
app.use(errorHandler);

router
  .get('/echo', (ctx) => {
    ctx.body = ctx.request.query.str;
  })
  .all('/proxy/*', proxy)
  .get('/*', async (ctx) => {
    if (ctx.path === '/') {
      ctx.path = 'index.html';
    }
    await send(ctx, path.join('/dist', ctx.path));
  });

app.use(router.routes());

export default app;

